package com.bestcem.xm.qdes.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.deliver.grpc.v1.services.*;
import com.bestcem.xm.qdes.convert.grpc.QdesGrpcConvertMapper;
import com.bestcem.xm.qdes.grpc.client.dto.WechatDeliverInfoDto;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Component
public class DeliverGrpcClient {

    private final com.bestcem.xm.qdes.convert.grpc.QdesGrpcConvertMapper grpcConvertMapper;

    @GrpcClient(GrpcClientNames.DS_DELIVER)
    private BpDeliverServiceGrpc.BpDeliverServiceBlockingStub stub;

    public DeliverGrpcClient(QdesGrpcConvertMapper grpcConvertMapper) {
        this.grpcConvertMapper = grpcConvertMapper;
    }

    public WechatDeliverInfoDto queryWechatDeliverByParam(String orgId, String surveyId) {
        QueryWechatDeliverRequest.Builder builder = QueryWechatDeliverRequest.newBuilder();
        builder.setSurveyId(surveyId);
        builder.setOrgId(orgId);
        try {
            QueryWechatDeliverResponse response = stub.queryWechatDeliverByParam(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[qdes] deliver getWechatDeliver orgId={}，surveyId={} Error; ErrorMsg: {}", orgId, surveyId, response);
                return null;
            }
            WechatDeliverInfo info = response.getWechatDeliverInfo();
            return grpcConvertMapper.toWechatDeliverInfoDto(info);
        } catch (Exception e) {
            log.error("[qdes] ex deliver getWechatDeliver orgId={}，surveyId={} Error; ErrorMsg: {}", orgId, surveyId, e.getMessage());
            return null;
        }
    }

    public String createWechatdeliverWithName(String orgId, String userId, String surveyId, String name) {
        CreateWechatdeliverWithNameRequest.Builder builder = CreateWechatdeliverWithNameRequest.newBuilder();
        builder.setOrgId(orgId);
        builder.setUserId(userId);
        builder.setSurveyId(surveyId);
        builder.setName(name);
        try {
            CreateWechatdeliverWithNameResponse response = stub.createWechatdeliverWithName(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[qdes] deliver createWechatdeliverWithName orgId={}，surveyId={} Error; ErrorMsg: {}", orgId, surveyId, response);
                return null;
            }
            return response.getDeliverId();
        } catch (Exception e) {
            log.error("[qdes] ex deliver createWechatdeliverWithName orgId={}，surveyId={} Error; ErrorMsg: {}", orgId, surveyId, e.getMessage());
            return null;
        }
    }

    public void createDefaultDeliverBase(String projectId, String userId, String orgId) {
        AddDefaultDeliverBaseRequest.Builder builder = AddDefaultDeliverBaseRequest.newBuilder();
        builder.setProjectId(projectId);
        builder.setUserId(userId);
        builder.setOrgId(orgId);
        try {
            AddDefaultDeliverBaseResponse response = stub.addDefaultDeliverBase(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[qdes] deliver createDefaultDeliverBase projectId={}，userId={} Error; ErrorMsg: {}", projectId, userId, response);
            }
        } catch (Exception e) {
            log.error("[qdes] ex deliver createDefaultDeliverBase projectId={}，userId={} Error; ErrorMsg: {}", projectId, userId, e.getMessage());
        }
    }

    public void createDefaultDeliverStore(String projectId, String userId, String orgId) {
        AddDefaultDeliverStoreRequest.Builder builder = AddDefaultDeliverStoreRequest.newBuilder();
        builder.setProjectId(projectId);
        builder.setUserId(userId);
        builder.setOrgId(orgId);
        try {
            AddDefaultDeliverStoreResponse response = stub.addDefaultDeliverStore(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[qdes] deliver createDefaultDeliverStore projectId={}，userId={} Error; ErrorMsg: {}", projectId, userId, response);
            }
        } catch (Exception e) {
            log.error("[qdes] ex deliver createDefaultDeliverStore projectId={}，userId={} Error; ErrorMsg: {}", projectId, userId, e.getMessage());
        }
    }

    public String getWxShareQrcodeImgurl(String orgId, String surveyId, String deliverId, String orgCode, Map<String, String> kwargs) {
        GetWxShareQrcodeImgurlRequest.Builder builder = GetWxShareQrcodeImgurlRequest.newBuilder();
        builder.setOrgId(orgId);
        builder.setSurveyId(surveyId);
        builder.setDeliverId(deliverId);
        log.info("getWxShareQrcodeImgurl client orgCode=" + orgCode);
        builder.setOrgCode(orgCode);
        builder.putAllKwargs(kwargs);
        try {
            GetWxShareQrcodeImgurlResponse response = stub.getWxShareQrcodeImgurl(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[qdes] deliver getWxShareQrcodeImgurl {}", response);
            }
            return response.getWxshareQrcodeImg();
        } catch (Exception e) {
            log.error("[qdes] ex deliver getWxShareQrcodeImgurl  ErrorMsg: {}", e.getMessage());
            return null;
        }
    }

    public void projectDeletedShare(List<String> project_ids, String survey_id) {
        ProjectDeletedShareRequest.Builder builder = ProjectDeletedShareRequest.newBuilder();
        builder.addAllProjectIds(project_ids);
        builder.setSurveyId(survey_id);
        try {
            ProjectDeletedShareResponse response = stub.projectDeletedShare(builder.build());
            if (Objects.isNull(response) || response.getStatus().getCodeValue() != 0) {
                log.error("[qdes] deliver projectDeletedShare project_ids={}，survey_id={} Error; ErrorMsg: {}", project_ids, survey_id, response);
            }
        } catch (Exception e) {
            log.error("[qdes] ex deliver projectDeletedShare project_ids={}，survey_id={} Error; ErrorMsg: {}", project_ids, survey_id, e.getMessage());
        }
    }
}
